/**
 * @author dxp
 * 用于一些公共的方法判断
 */
/***********token start***********/
//设置token
function setToken(token) {
    var storage = window.localStorage;
    if (!storage) {
        alert("浏览器不支持localstorage");
        return false;
    }
    storage.setItem("token", token);
}

//获取token
function getToken() {
    var storage = window.localStorage;
    var token = storage.token;//token的值
    return token;
}

//清除token
function clearToken() {
    var storage = window.localStorage;
    storage.setItem("token", null);
    goLogin();
}

//跳转登录页
function goLogin() {
    //alert("认证失败");
    //clearToken();
    window.top.location = "login.html";
    return;
}

$.ajaxSetup({
    headers: {'token': getToken() != null ? getToken() : ''},
    complete: function (xhr) {
        if (xhr.responseJSON.code == "444") {
            console.log("登录失效！");
            goLogin();
        }
    },
    beforeSend: function (xhr) {
        //alert(getToken());
        xhr.setRequestHeader('token', getToken());
        if (getToken()!=null){
            xhr.setRequestHeader('token',getToken());
        }else {
            goLogin();
        }
    },
})
/***********token end***********/
